package com.tydic.assistant.config;

import com.tydic.assistant.util.FileUtil;
import com.tydic.assistant.util.SerialPortManager;
import gnu.util.DLLPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * Created by maliang on 2017/5/10.
 */
public class COMWindow {
    private static  final Logger logger = LoggerFactory.getLogger(COMWindow.class);
    private JPanel configWin;
    private JComboBox BAUDRATE;
    private JComboBox DATABITS;
    private JComboBox STOPBITS;
    private JComboBox PARITY;
    private JTextField WEIGHTINTSTART;
    private JTextField WEIGHTINTEND;
    private JButton submit;
    private JButton cancel;
    private JComboBox PORTNAME;
    private JCheckBox modify;
    public COMWindow(){
        //复制文件
        String destFilePath = DLLPath.getDllPath("rxtxParallel.dll");
        FileUtil.copyFileFromClsPath("rxtxParallel.dll",destFilePath);
        destFilePath = DLLPath.getDllPath("rxtxSerial.dll");
        FileUtil.copyFileFromClsPath("rxtxSerial.dll",destFilePath);
    }

    public static void main(String[] args) {
        COMWindow portConfig = new COMWindow();
        JFrame frame = new JFrame("配置");
        frame.setContentPane(portConfig.configWin);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // frame.pack();
        frame.setSize(500, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        //设置默认值
        ArrayList<String> portList = SerialPortManager.findPort();
        for(String port:portList){
            portConfig.PORTNAME.addItem(port);
        }
        portConfig.BAUDRATE.setSelectedItem("9600");
        portConfig.DATABITS.setSelectedItem("8");
        portConfig.STOPBITS.setSelectedItem("1");
        portConfig.PARITY.setSelectedItem("NONE");
        //确定按钮
        portConfig.submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean result  = portConfig.createCOMConfigFile(portConfig);
                JOptionPane.showMessageDialog(null, "端口设置成功", "提示信息",JOptionPane.WARNING_MESSAGE);
                System.exit(0);
            }
        });
        //取消按钮
        portConfig.cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        //修改复选框
        portConfig.modify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean editAble = false;
                if (portConfig.modify.isSelected()) {
                    editAble = true;
                }
                portConfig.BAUDRATE.setEnabled(editAble);
                portConfig.DATABITS.setEnabled(editAble);
                portConfig.STOPBITS.setEnabled(editAble);
                portConfig.PARITY.setEnabled(editAble);
                portConfig.WEIGHTINTSTART.setEditable(editAble);
                portConfig.WEIGHTINTEND.setEditable(editAble);
            }
        });
    }

    /**
     * 生成串口的配置文件
     * @param portConfig
     * @return
     */
    public boolean createCOMConfigFile(COMWindow portConfig) {
        ArrayList<String> content = new ArrayList<String>();
        StringBuffer portNameBuf = new StringBuffer();
        portNameBuf.append("PORTNAME=");
        portNameBuf.append(portConfig.PORTNAME.getSelectedItem().toString());
        content.add(portNameBuf.toString());

        StringBuffer baudrateBuf = new StringBuffer();
        baudrateBuf.append("BAUDRATE=");
        baudrateBuf.append(portConfig.BAUDRATE.getSelectedItem().toString());
        content.add(baudrateBuf.toString());

        StringBuffer databitsBuf = new StringBuffer();
        databitsBuf.append("DATABITS=");
        databitsBuf.append(portConfig.DATABITS.getSelectedItem().toString());
        content.add(databitsBuf.toString());

        StringBuffer stopbitsBuf = new StringBuffer();
        stopbitsBuf.append("STOPBITS=");
        stopbitsBuf.append(portConfig.STOPBITS.getSelectedItem().toString());
        content.add(stopbitsBuf.toString());

        StringBuffer parityBuf = new StringBuffer();
        String parityStr = portConfig.PARITY.getSelectedItem().toString();
        String parityValue = "";
        switch (parityStr) {
            case "NONE":
                parityValue = "0";
                break;
            case "ODD":
                parityValue = "1";
                break;
            case "EVEN":
                parityValue = "2";
                break;
            case "MARK":
                parityValue = "3";
                break;
            case "SPACE":
                parityValue = "4";
                break;
        }
        parityBuf.append("PARITY=");
        parityBuf.append(parityValue);
        content.add(parityBuf.toString());

        StringBuffer weightintstartBuf = new StringBuffer();
        weightintstartBuf.append("WEIGHTINTSTART=");
        weightintstartBuf.append(portConfig.WEIGHTINTSTART.getText());
        content.add(weightintstartBuf.toString());

        StringBuffer weightintendBuf = new StringBuffer();
        weightintendBuf.append("WEIGHTINTEND=");
        weightintendBuf.append(portConfig.WEIGHTINTEND.getText());
        content.add(weightintendBuf.toString());
        //解析路径
        String destFilePath = System.getProperty("java.home").split(":")[0] + ":/loadometerParserConfig";
        destFilePath = destFilePath + "/comPortConfig.properties";
        FileUtil.writeFile(content, destFilePath, false);
        return true;
    }
}
